/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asistencia.DAO;

import com.asistencia.DAO.utils.Connector;
import com.asistencia.DAO.utils.InterfaceDAO;
import com.asistencia.model.Modulo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author rfarias
 */
public class ModuloDAO implements InterfaceDAO {

    public List findAll() {
        Modulo mod = null;
        ArrayList<Modulo> listamod = new ArrayList<Modulo>();
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from Modulos");
            while(rs.next())
            {
                mod = new Modulo(
                        rs.getString("descripcion"),
                        rs.getInt("ruta")
                        );
                mod.setId_Modulo(rs.getInt("idmodulo"));
                listamod.add(mod);
            }
        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: findAll - Exception: " + ex.getMessage());
        } finally {
        return listamod;
        }
    }



    public Object searchById(String id) {
        return null;
    }

    public Object searchById(int id) {
        Modulo mod = null;
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from modulos where idmodulo = " + id);
            if(rs.next()) {
                mod = new Modulo(rs.getString("descripcion"), rs.getInt("ruta"));
            }
                mod.setId_Modulo(id);
        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: findAll - Exception: " + ex.getMessage());
        } finally {

            return mod;
        }
    }


    public Object update(Object obj) {
        Modulo mod = (Modulo)obj;
        Statement st = Connector.getStatement();
        StringBuilder sb = new StringBuilder();
        sb.append("update Modulos set ");
        sb.append ("descripcion='");sb.append(mod.getDescripcion());sb.append("', ");
        sb.append ("ruta=");sb.append(mod.getRuta());sb.append(" where id=");sb.append(mod.getId_Modulo());
        try {
            int res = st.executeUpdate(sb.toString());
            if (res==1)
            {
                return mod;
            }
            else
            {
                return null;
            }
        } catch (SQLException ex) {
           System.out.println("ERROR: ModuloDAO - Method: update - Exception: " + ex.getMessage());
           return null;
        }

    }

    public Object insert(Object obj) {
        Modulo mod = (Modulo)obj;
        try{
            StringBuilder sb = new StringBuilder();
            sb.append("insert into Modulos (descripcion, ruta) values(");
            sb.append("'");sb.append(mod.getDescripcion());sb.append("',");
            sb.append("");sb.append(mod.getRuta());sb.append("' )");

            Statement st = Connector.getStatement();
            int res = st.executeUpdate(sb.toString(), Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = st.getGeneratedKeys();
            if(rs.next()) {
                mod.setId_Modulo(rs.getInt(1));
            }

        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: insert - Exception: " + ex.getMessage());
        } finally {
            return String.valueOf(mod.getId_Modulo()) != null ? searchById(mod.getId_Modulo()) : null;
        }

    }

 public boolean delete(Object obj) {
        Modulo mod = (Modulo) obj;
        Statement st = Connector.getStatement();
        try {
            int res = st.executeUpdate("delete from modulos where idmodulo="+mod.getId_Modulo()+" ");
             if (res>0)
                {
                   return true;
                }
             else
                {
                    return false;
                }
        } catch (SQLException ex) {
            System.out.println("ERROR: ModuloDAO - Method: delete - Exception: " + ex.getMessage());
            return false;
        }
    }


  public Object searchByDesc(String desc) {
        Modulo mod = null;
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from modulos where descripcion= " + desc);
            if(rs.next()) {
                mod = new Modulo(rs.getString("descripcion"), rs.getInt("ruta"));
            }
                mod.setId_Modulo(rs.getInt("id_modulo"));
        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: findAll - Exception: " + ex.getMessage());
        } finally {

            return mod;
        }
    }
  
  
  public List<String> listaRutas ()
  {
      List<String> rutas = new ArrayList<String>();
      try{
            ResultSet rs = Connector.getStatement().executeQuery("SELECT DISTINCT ruta FROM modulos ORDER BY ruta");
            //System.out.println(rs.getFetchSize());
            while(rs.next()) {
           //     System.out.println(rs.getInt("ruta"));
                rutas.add(String.valueOf(rs.getInt("ruta")));
            }
        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: ListaRutas - Exception: " + ex.getMessage());
        } finally {
 //           System.out.println(rutas.size());
            return rutas;
        }
  }
  
   public List SearcByRuta(int id) {
        Modulo mod = null;
        ArrayList<Modulo> listamod = new ArrayList<Modulo>();
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from Modulos where ruta='"+id+"'");
            while(rs.next())
            {
                mod = new Modulo(
                        rs.getString("descripcion"),
                        rs.getInt("ruta")
                        );
                mod.setId_Modulo(rs.getInt("idmodulo"));
                listamod.add(mod);
            }
        } catch (Exception ex){
            System.out.println("ERROR: ModuloDAO - Method: SerachByRuta - Exception: " + ex.getMessage());
        } finally {
        return listamod;
        }
    }


}
